package com.fan.controller;

import com.google.common.collect.Lists;
import com.fan.entity.EventCert;
import com.fan.service.EventCertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class EventCertController {

    @Autowired
    EventCertService eventCertService;

    List<EventCert> list = Lists.newArrayList();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @PostConstruct
    public void initData() throws ParseException {
        // 2021-10-22 10:22:22
        Date date1 = new Date(1634869342000L);
        // 2022-01-22 10:22:22
        Date date2 = new Date(1642818142000L);
        // 2022-04-22 10:22:22
        Date date3 = new Date(1650594142000L);
        // 2022-07-22 10:22:22
        Date date4 = new Date(1658456542000L);
        // 2022-10-22 10:22:22
        Date date5 = new Date(1666405342000L);
        // 2023-10-22 10:22:22
        Date date6 = new Date(1697941342000L);

        Date date11 = sdf.parse("2021-10-22 10:32:22");
        Date date22 = sdf.parse("2022-01-22 10:32:22");
        Date date33 = sdf.parse("2022-04-22 10:32:22");
        Date date44 = sdf.parse("2022-07-22 10:32:22");
        Date date55 = sdf.parse("2022-10-22 10:32:22");
        Date date66 = sdf.parse("2023-10-22 10:32:22");

        list.add(new EventCert(2745955729255747637L, 2516110240115745045L, 2516110240115745045L, "2589631",
                2745955797428081128L, "test", "THIRD_COMPANY", "ENTERPRISE", "name1", "myca",
                "506cf03c-f43b-46d0-a39f-d9f61d7a19aa", "5f69c3a1", date1, date11,
                date1, "RSA", -1162831176));
        list.add(new EventCert(2745955729255747638L, 2516110240115745045L, 2516110240115745045L, "2589631",
                2745955797428081128L, "test", "THIRD_COMPANY", "ENTERPRISE", "name1", "myca",
                "506cf03c-f43b-46d0-a39f-d9f61d7a19aa", "5f69c3a1", date2, date22,
                date2, "RSA", -1265189056));
        list.add(new EventCert(2745955729255747639L, 2516110240115745045L, 2516110240115745045L, "2589631",
                2745955797428081128L, "test", "THIRD_COMPANY", "ENTERPRISE", "name1", "myca",
                "506cf03c-f43b-46d0-a39f-d9f61d7a19aa", "5f69c3a1", date3, date33,
                date3, "RSA", -2028109733));
        list.add(new EventCert(2745955729255747640L, 2516110240115745045L, 2516110240115745045L, "2589631",
                2745955797428081128L, "test", "THIRD_COMPANY", "ENTERPRISE", "name1", "myca",
                "506cf03c-f43b-46d0-a39f-d9f61d7a19aa", "5f69c3a1", date4, date44,
                date4, "RSA", -1472654854));
        list.add(new EventCert(2745955729255747641L, 2516110240115745045L, 2516110240115745045L, "2589631",
                2745955797428081128L, "test", "THIRD_COMPANY", "ENTERPRISE", "name1", "myca",
                "506cf03c-f43b-46d0-a39f-d9f61d7a19aa", "5f69c3a1", date5, date55,
                date5, "RSA", -1472654854));
        list.add(new EventCert(2745955729255747642L, 2516110240115745045L, 2516110240115745045L, "2589631",
                2745955797428081128L, "test", "THIRD_COMPANY", "ENTERPRISE", "name1", "myca",
                "506cf03c-f43b-46d0-a39f-d9f61d7a19aa", "5f69c3a1", date6, date66,
                date6, "RSA", -1472654854));
    }

    @PostMapping("/batchIncert")
    public int saveEventCert() {
        return eventCertService.batchIncertInfo(list);
    }

    @GetMapping("/getByHashCode")
    public EventCert getByHashCode(int hashcode, Long contractId, String createTime, String endTime) throws ParseException {
        Date ct = sdf.parse(createTime);
        Date et = sdf.parse(endTime);
        return eventCertService.findByHashCode(hashcode, contractId, ct, et);
    }

    @GetMapping("/selectAll")
    public List<EventCert> selectAll() {
        return eventCertService.selectAll();
    }

    @GetMapping("/delAll")
    public int delAll() {
        return eventCertService.delAll();
    }

    @GetMapping("/findByCreateTime")
    public EventCert findByCreateTime(String createTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = sdf.parse(createTime);
        return eventCertService.findByCreateTime(parse);
    }

    @GetMapping("/selectRange")
    public List<EventCert> selectRange(int hashcode, Long bizId, String st, String et) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date std = sdf.parse(st);
        Date etd = sdf.parse(et);
        return eventCertService.selectRange(hashcode, bizId, std, etd);
    }
}
